import requests
import time

# 要请求的 URL
#url = 'http://movie.tc.skysrt.com/recommend/discovery/news/list?type=classified&category=news_news_ent&dayOffset=1'
#url = 'http://movie.tc.skysrt.com/recommend/discovery/news/list?type=classified&category=news_news_ent'
#url = 'http://movie.tc.skysrt.com/pre/recommend/recommend/discovery/news/list?type=classified&category=news_news_ent'
url = 'http://movie-pre.tc.skysrt.com/recommend/discovery/news/list?type=classified&category=news_news_ent'
#url = 'http://localhost:8080/recommend/discovery/news/list?type=classified&category=news_news_ent&dayOffset=1'

# 创建一个空列表来存储每次请求的耗时
elapsed_times = []

# 循环调用10次
for i in range(50):
    # 记录开始时间
    start_time = time.time()

    # 发送 GET 请求
    response = requests.get(url)

    # 确保请求成功（可选，但推荐）
    response.raise_for_status()  # 如果请求失败，将引发HTTPError异常

    # 记录结束时间
    end_time = time.time()

    # 计算耗时（秒）
    elapsed_time = end_time - start_time

    # 将耗时添加到列表中
    elapsed_times.append(elapsed_time)

    # 打印每次请求的耗时（可选）
    print(f'Request {i + 1}: Elapsed Time = {elapsed_time:.4f} seconds')
    #print(f'code:{response.text}')

# 打印所有请求的平均耗时
average_elapsed_time = sum(elapsed_times) / len(elapsed_times)
print(f'Average Elapsed Time over 10 requests: {average_elapsed_time:.4f} seconds')